package jsave.action;

import java.io.File;

import jsave.exception.JSaveArretSauvegarde;
import jsave.exception.JSaveException;
import jsave.tree.FileNode;

import org.apache.log4j.Logger;

public class ActionCopier extends Action {

	private static final Logger log = Logger.getLogger(ActionCopier.class);

	public ActionCopier(int typeAction) {

		super();

		if (typeAction == Action.ACTION_A || typeAction == Action.ACTION_B) {
			this.typeAction = typeAction;
		} else {
			log.error("ERREUR - Le type d'action '" + typeAction
					+ "' ne correspond pas a l'action 'ActionCopier' ");
		}
	}

	public void executer(File s, File d) throws JSaveArretSauvegarde {

		if (typeAction == Action.ACTION_A) { // ACTION_A
			if (s.isDirectory())
				creerDossier(d);
			else
				ajouterFichier(s, d);
		} else { // ACTION_B
			if (d.isDirectory())
				creerDossier(s);
			else
				ajouterFichier(d, s);
		}
	}

	public String getNom() {
		return "COPIER";
	}

	@Override
	public void executer(String sourcePath, String destinatinoPath, FileNode n)
			throws JSaveException {

		if (typeAction == Action.ACTION_B) {
			if (n.isDirectory())
				creerDossier(new File(sourcePath + File.separator
						+ n.getRelativePath()));
			else
				ajouterFichier(new File(destinatinoPath + File.separator
						+ n.getRelativePath()), new File(sourcePath
						+ File.separator + n.getRelativePath()));
		} else {
			throw new JSaveException("Methode non implementee");
		}
	}
}
